from rest_framework import serializers
from .models import CourseCategory, Course, Teacher


class CourseCategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = CourseCategory
        fields = ['id', 'name']


class TeacherModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Teacher
        fields = ['name', 'title', 'signature']


class CourseModelSerializer(serializers.ModelSerializer):
    # 课程表与老师表是有关系的，多对一的关系
    # teacher_name = serializers.CharField(max_length=64, source='teacher.name')

    # 对老师序列化器实例化一个对象
    # teacher = TeacherModelSerializer(many=True)  # 当前类是多对一中的一，需要加many=True
    teacher = TeacherModelSerializer()  # 当前类是多对一中的多，不用加many=True

    class Meta:
        model = Course
        fields = ["id", "name", "course_img", "students", "lessons", "pub_lessons", "price", 'teacher', 'lesson_list']
